package taller2.view.menus;

import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;

import taller2.controller.listeners.UserImputListener;
import taller2.interfaces.HidableWindow;
import taller2.utils.Constants;
import taller2.utils.Utils;
import taller2.view.components.MyHighlighter;

public class UserImputMenu extends JPanel {
	private static final long serialVersionUID = 1L;
    
	private String solution;
	private JTextArea initial,request,request2,response;
    private MyHighlighter highlighter;
    private JScrollPane scrollBar,scrollBar2;
    
    private JToggleButton acceptButton;
    private JToggleButton cancelButton;
    
	public UserImputMenu(HidableWindow window, String req, String req2, String InitialResp, String sol, UserImputListener listener){
		solution = sol;
		
		request = new JTextArea(req);
		request.setMargin(new Insets(0,0,0,0));
		request.setEditable(false);
		request.setFont(new Font("Serif",Font.PLAIN,(int)(Constants.fontSize*1.5)));

		initial = new JTextArea(InitialResp);
		initial.setMargin(new Insets(0,0,0,0));
		initial.setEditable(false);
		initial.setFont(new Font("Serif",Font.PLAIN,(int)(Constants.fontSize*1.5)));

        scrollBar = new JScrollPane(initial);
        scrollBar.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);                  		
		
		request2 = new JTextArea(req2);
		request2.setMargin(new Insets(0,0,0,0));
		request2.setEditable(false);
		request2.setFont(new Font("Serif",Font.PLAIN,(int)(Constants.fontSize*1.5)));
		
		response = new JTextArea(InitialResp);
		response.setMargin(new Insets(0,0,0,0));
		response.setEditable(true);
		response.setFont(new Font("Serif",Font.PLAIN,(int)(Constants.fontSize*1.5)));
       
        highlighter = new MyHighlighter(response);
       
        scrollBar2 = new JScrollPane(response);
        scrollBar2.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollBar2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);                  
        
        acceptButton = new JToggleButton("accept");
        cancelButton = new JToggleButton("cancel");

        listener.set(this,window);
        
        acceptButton.addActionListener(listener);
        cancelButton.addActionListener(listener);
        
        setLayout(new GridBagLayout());
        
        JPanel panel = new JPanel(new GridBagLayout());
        Utils.addComponent(panel,acceptButton,0.5,1,new taller2.utils.Point(0,0));
        Utils.addComponent(panel,cancelButton,0.5,1,new taller2.utils.Point(1,0));        
       
        Utils.addComponent(this,request,1,0,new taller2.utils.Point(0,0));
        Utils.addComponent(this,scrollBar,1,1,new taller2.utils.Point(0,1));
        Utils.addComponent(this,request2,1,0,new taller2.utils.Point(0,2));
        Utils.addComponent(this,scrollBar2,1,1,new taller2.utils.Point(0,3));
        Utils.addComponent(this,panel,1,0,new taller2.utils.Point(0,4));
	}
	
	public String getSolution(){
		return solution;
	}
	
	public String getResponse(){
		return response.getText();
	}
	
	public boolean isAccept(JToggleButton button){
		return (acceptButton == button);
	}

	public boolean isCancel(JToggleButton button){
		return (cancelButton == button);
	}
	
	public void highlightLine(int line){
		highlighter.highlightLine(line);
	}
	
    // unselects all buttons
    public void unselectButtons(){
    	acceptButton.setSelected(false);
    	cancelButton.setSelected(false);
    } 
}
